package Task25;

import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class MySAXParser extends DefaultHandler {

        private LinkedList<String> data = new LinkedList<String>();
        private String tempVal;
        private String lastQName;
        private int level = 0;

        public MySAXParser(String filePath) {
        	File file = new File(filePath);
                parseDocument(file);
        }

        public String[] getResult() {
        	String[] returnData = new String[data.size()];
        	data.toArray(returnData);
                return returnData;
        }

        private void parseDocument(File file) {
                //get a factory
                SAXParserFactory parserFactory = SAXParserFactory.newInstance();
                try {
                        //get a new instance of parser
                        SAXParser parser = parserFactory.newSAXParser();
                        //parse the file and also register this class for call backs
                        parser.parse(file.toURI().toASCIIString(),this);
                        
                } catch (SAXException e) {
                	data.add("error");
                        //e.printStackTrace();
                } catch (ParserConfigurationException e) {
                	data.add("error");
                        //e.printStackTrace();
                } catch (IOException e) {
                	data.add("error");
                        //e.printStackTrace();
                }
        }

        //Event Handlers
        public void startElement(String uri, String localName, String qName,
                        Attributes attributes) throws SAXException {
                //reset
                String sAtr = "";
                for (int i = 0; i < attributes.getLength(); i++) {
                        String name = attributes.getLocalName(i);
                        String value = attributes.getValue(i);

                        sAtr += " " + name + "=" + value + " ";
                }
                data.add(getShift()+"<" + qName + sAtr + ">");
                tempVal = "";
                lastQName = qName;
                level++;
        }

        public void characters(char[] ch, int start, int length)
                        throws SAXException {
                tempVal = new String(ch, start, length);
        }

        public void endElement(String uri, String localName, String qName)
                        throws SAXException {
                if (lastQName.equalsIgnoreCase(qName)) {
                        String lastValue = data.remove(data.size() - 1);
                        data.add(lastValue + tempVal + "</" + qName + ">");
                        level--;
                } else {
                        level--;
                        data.add(getShift()+"</" + qName + ">");
                }
        }
        
        private String getShift(){
                String shift = "";
                for (int i = 0; i < level; i++)
                        shift += "   ";
                return shift;
        }
}